﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using OPCAutomation;

namespace OpcDaDemo
{
    class Program
    {
        static Dictionary<int, string> dic = new Dictionary<int, string>() { };
        static void Main(string[] args)
        {
            OPCServer daServer = new OPCServer();
            daServer.Connect("Kepware.KEPServerEX.V6");
            OPCGroups groups = daServer.OPCGroups;
            groups.DefaultGroupDeadband = 0;
            groups.DefaultGroupIsActive = true;
            var group = groups.Add("groups");
            OPCItems items = group.OPCItems;
            Array mid;
            Array mid2;
            Array mid3;
            Array mid4;

            dic.Add(0, "0");
            dic.Add(1, "通道 1.设备 2.标记3");
            dic.Add(2, "通道 1.设备 2.标记5");
            mid = dic.Values.ToArray();
            mid2 = dic.Keys.ToArray();
            group.DataChange += Group_DataChange;
            group.AsyncReadComplete += Group_AsyncReadComplete; ;
            items.AddItems(2, ref mid, ref mid2, out mid3, out mid4);
            group.DeadBand = 0;
            group.IsActive = true;
            group.IsSubscribed = true;
            group.UpdateRate = 250;
           // var testItem = items.AddItem("通道 1.设备 2.标记2", 1);

            List<int> mids = new List<int>();
            foreach (var midss in mid3)
            {
                mids.Add(Convert.ToInt32(midss));
            }

            Array ass = mids.ToArray();
            //group.AsyncRead(3,ref ass,out mid4,0,out int cancel);
            Task.Run(() =>
            {
                while (true)
                {
                    try
                    {
                        Thread.Sleep(2000);
                        
                        //testItem.Read(1, out object a, out object b, out object c);
                        //item.Read(1, out object a, out object b, out object c);
                        //Console.WriteLine(a.ToString() + "******" + b.ToString() + "*****" + c.ToString());
                    }
                    catch (Exception e)
                    {
                        Console.WriteLine(e);

                    }

                }
            });
            Console.WriteLine("**************");
            Console.ReadKey();
            daServer.Disconnect();
        }

        private static void Group_AsyncReadComplete(int TransactionID, int NumItems, ref Array ClientHandles, ref Array ItemValues, ref Array Qualities, ref Array TimeStamps, ref Array Errors)
        {

        }

        private static void Group_DataChange(int TransactionID, int NumItems, ref Array ClientHandles, ref Array ItemValues, ref Array Qualities, ref Array TimeStamps)
        {
            for (int i = 1; i < NumItems + 1; i++)
            {
                string name = "变量名称" + dic[(int)ClientHandles.GetValue(i)];
                Console.WriteLine(name);
                Console.WriteLine(ItemValues.GetValue(i).ToString());
                Console.WriteLine(Qualities.GetValue(i).ToString());
            }
            Console.WriteLine("***********************");
        }
    }
}
